<template>
  <div>
    <div style="padding: 10px">
      <el-card>
        <div class="body">
          <elform2
            :searchData="searchData"
            :status="status"
            :label="'目录名称'"
            :newwhat="'新增目录'"
            :placeholder="''"
            @resetField="resetField"
            @search="search"
            @add="add"
          ></elform2>

          <elalert :counts="counts"></elalert>

          <el-table :data="list" style="width: 100%">
            <el-table-column type="index" label="序号" width="80px" />
            <el-table-column prop="subjectName" label="所属学科" />
            <el-table-column prop="directoryName" label="目录名称" />
            <el-table-column prop="username" label="创建者" />
            <el-table-column prop="addDate" label="创建日期" width="165px" />
            <el-table-column prop="totals" label="面试题数量" />
            <el-table-column prop="state" label="状态" />
            <el-table-column label="操作" width="150" class="btncell">
              <template #default="{ row }">
                <el-button type="text" @click="forbidden(row)">
                  {{ row.state === '已启用' ? '禁用' : '启用' }}</el-button
                >
                <el-button
                  type="text"
                  @click="edit(row)"
                  :disabled="row.state === '已启用' ? true : false"
                  >修改
                </el-button>
                <el-button
                  type="text"
                  style="margin-left: 9px"
                  @click="del(row)"
                >
                  删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <elpagination
            :counts="counts"
            :data="searchData"
            :n="[5, 10, 20, 50]"
            @handleSizeChange="handleSizeChange"
            @handleCurrentChange="handleCurrentChange"
          ></elpagination>
        </div>
      </el-card>
    </div>
    <!-- 添加、修改学科的弹窗 -->
    <el-dialog
      :visible.sync="showDialog"
      width="400px"
      :title="title"
      @close="closeDialog"
    >
      <el-form ref="addRef" :model="dialogData" :rules="rules">
        <el-form-item
          label="所属学科"
          label-width="80px"
          prop="subjectID"
          style="margin-bottom: 20px"
        >
          <el-select v-model="dialogData.subjectID" style="width: 280px">
            <el-option
              v-for="item in subjectID"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="目录名称" label-width="80px" prop="directoryName">
          <el-input
            v-model="dialogData.directoryName"
            style="width: 280px"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>

      <div align="right">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submitCheck">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
import { remove, update, add, changeState, detail } from '@/api/hmmm/directorys'
import elalert from '@/components/elalert'
import elpagination from '@/components/elpagination/index.vue'
import elform2 from '@/components/form2'
import { status } from '@/api/hmmm/constants'
import moment from 'moment'
export default {
  data() {
    return {
      counts: 0,
      status: status,
      subjectID: [],
      searchData: {
        directoryName: '',
        page: 1,
        pagesize: 10,
        state: null
      },
      list: [],
      showDialog: false,
      title: '新增学科',
      dialogData: {
        subjectID: null,
        directoryName: null,
        id: null
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      direcArr: []
    }
  },
  components: { elalert, elpagination, elform2 },
  created() {
    console.log(this.$route.query.id)
    this.searchData.subjectID = this.$route.query.id
    this.getSimple()
    this.getDirectoryList()
  },
  methods: {
    async getDirectoryList() {
      const { data } = await list(this.searchData)
      // console.log(data.items)
      data.items.forEach(item => {
        item.isFrontDisplay = item.isFrontDisplay === 1 ? '是' : '否'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
        item.state = item.state === 1 ? '已启用' : '已禁用'
      })
      if (JSON.parse(localStorage.getItem('direcArr'))) {
        data.items.forEach(item => {
          JSON.parse(localStorage.getItem('direcArr')).forEach(item2 => {
            item.id === item2.id ? (item.state = item2.state) : ''
          })
        })
      }
      this.list = data.items
      this.counts = data.counts
      this.searchData.page = data.page
      this.searchData.pagesize = data.pagesize
    },
    async getSimple() {
      const { data } = await simple()
      this.subjectID = data
    },
    resetField() {
      this.$refs.searchRef.resetFields()
    },
    handleSizeChange(val) {
      this.searchData.pagesize = val
      this.getDirectoryList()
    },
    handleCurrentChange(val) {
      this.searchData.page = val
      this.getDirectoryList()
    },
    search() {
      this.getDirectoryList()
    },
    add() {
      this.title = '新增目录'
      this.showDialog = true
    },
    async edit(row) {
      const { data } = await detail(row)
      this.dialogData = { ...data }
      this.title = '修改目录'
      row.isFrontDisplay === '是'
        ? (this.dialogData.isFrontDisplay = '1')
        : (this.dialogData.isFrontDisplay = '0')
      this.dialogData.subjectID = row.subjectID
      this.showDialog = true
    },
    async submitCheck() {
      try {
        await this.$refs.addRef.validate()
        if (this.title === '新增目录') {
          await add(this.dialogData)
          this.showDialog = false
          this.$message.success('添加成功')
          this.getDirectoryList()
        } else if (this.title === '修改目录') {
          await update(this.dialogData)
          this.showDialog = false
          this.$message.success('修改成功')
          this.getDirectoryList()
        }
      } catch (error) {
        console.log(error)
      }
    },
    closeDialog() {
      this.$refs.addRef.resetFields()
    },
    async forbidden(row) {
      row.state = row.state === '已启用' ? 1 : 0
      await changeState({ id: row.id, state: row.state })
      this.$message.success('操作成功')
      row.state = row.state === 0 ? '已启用' : '已禁用'
      this.list.forEach(item => {
        this.direcArr.push({ id: item.id, state: item.state })
      })
      localStorage.setItem('direcArr', JSON.stringify(this.direcArr))
    },
    async del(row) {
      this.$confirm('该项操作不可恢复！确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.$message.success('删除成功')
          this.getDirectoryList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.el-card {
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  .body {
    width: 100%;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB,
      Microsoft YaHei, Arial, sans-serif;

    ::v-deep .el-table {
      .el-table--fit {
        border-right: 0;
        border-bottom: 0;
      }
    }
    ::v-deep .el-table th.el-table__cell.is-leaf {
      border-bottom: 2px solid #e8e8e8;
      background-color: #fafafa;
    }
    ::v-deep .el-card .body[data-v-eacc1416] .el-table td.el-table__cell div {
      display: flex;
      padding-left: 5px;
      padding-right: 5px;
      box-sizing: border-box;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: normal;
      word-break: break-all;
      line-height: 23px;
    }
  }
}
</style>
